function [KFIn,Harvey]=DSGEtoGeneralizedFilter(tauVec,GG,RR,SDX,ZZ,CC,cummulatorType,quarter) 
% [KFIn,Harvey]=DSGEtoGeneralizedFilter(tauVec,GG,RR,SDX,ZZ,CC,cummulatorType,quarter) 
% 
% This function generates from DSGE and data choices all inputs needed to
% run GeneralizedKFilter.m and GeneralizedKFilterSmoother.m, which also
% allow for mixed frequencies. 
% 
% Part I. Takes the solution matrices of the DSGE and assigns them to the
% structure KFIn. If the model has breaks, the vector *tauVec* must be
% provided 
% 
% Part II. Generates Harvey structure in the case model is at mixed frequency  
%      
%          If there are no variables in Mixed Frequency, set
%          cummulatorType=[]; 
%
% Outputs 
% 
%
% 


Npages=size(GG,3); 
%% Case 1:  No breaks 
if Npages==1 

    %% Matrices of Measurement equation 
    KFIn.Z=ZZ;
    KFIn.H=[];
    if size(CC,2)~=1 
        error('Model with no breaks, C must be [Ns 1]') 
    end 
    KFIn.d=ZZ*CC;
    
    %% Matrices of the Transition equation 
    KFIn.T=GG; 
    KFIn.R=RR; 
    KFIn.Q=SDX'*SDX; 
    KFIn.c=zeros(size(GG,1),1); 
            
else
    %% Case 2: Breaks 
    
    if isempty(tauVec)==true 
        error('Model has breaks but tauVec is empty'); 
    end 
    tauUnique=unique(tauVec); 
    Nbreaks=length(tauUnique); 
    NperBreak=zeros(Nbreaks); 
    for ii=1:Nbreaks 
        NperBreak(ii)=length( find( tauVec == tauUnique(ii) ) ); 
        fprintf('N Obs in sample %d = %d \n',ii,NperBreak(ii) ); 
    end                         
    
    %% Matrices of the measurement equation
    % Z selection matrix for observables
    KFIn.Z.Zt=ZZ;
    KFIn.Z.tauZ=tauVec;
    % H covariance for measurement errors
    KFIn.H=[];
    % d constants
    if size(CC,2)~=Npages 
        error('Model with Nb breaks, C must be [Ns Nb]'); 
    end 
    KFIn.d.dt=zeros( [size(ZZ,1) Npages] );
    % Filled in loop below
    KFIn.d.taud=tauVec;
        
    %% Matrices of the Transition equation     
    % Transition 
    KFIn.T.Tt=GG; 
    KFIn.T.tauT=tauVec; 

    % Multiplies innovations 
    KFIn.R.Rt=RR; 
    KFIn.R.tauR=tauVec; 
    
    % Covariance of innovations
    KFIn.Q.Qt=zeros( size(SDX) );
    % Filled in loop below
    KFIn.Q.tauQ=tauVec;
    
    for ii=1:Npages
        KFIn.d.dt(:,ii)=ZZ(:,:,ii)*CC(:,ii);
        KFIn.Q.Qt(:,:,ii)=squeeze( SDX(:,:,ii) )'*( squeeze( SDX(:,:,ii) ) );
    end
    
    KFIn.c=zeros( size(GG,1) , Nbreaks ); 

end 

if isempty( cummulatorType )==true 
    Harvey=[]; 
else 
    cummulatorType=( cummulatorType(:) )'; 
    if all(cummulatorType)==true 
        Harvey=[]; 
    else 
    Harvey=harvey_accumulator(quarter,3,cummulatorType); 
    end 
end 
 